﻿using Assets.Scripts.Manager;
using DG.Tweening;
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;

public class DeathView : MonoBehaviour
{
    #region
    private void OnEnable()
    {
        AddInputEvent();
    }
    private void OnDisable()
    {
        RemoveInputEvent();
    }
    #endregion
    [SerializeField] private Text _gameOverText;
    [SerializeField] private int _gameOverfadeInTime;

    [SerializeField] private int _intervalTime;

    [SerializeField] private Image _contextMask;
    [SerializeField] private int _contextTntervalTime;

    public void LoadGameOver() 
    {
        Sequence sequence = DOTween.Sequence();
        sequence.Append(_gameOverText.DOFade(1, _gameOverfadeInTime));
        sequence.AppendInterval(_intervalTime);
        sequence.Append(_contextMask.DOFade(0,_contextTntervalTime));
    }

    [SerializeField] private Image _continueIcon;


    [SerializeField] private Image _passwordIcon;


    private int _selectIndex = 0;

    public void Show(int i) 
    {
        switch (i)
        {
            case 0:
               
                _continueIcon.gameObject.SetActive(true);
                _passwordIcon.gameObject.SetActive(false);
                break;
            case 1:
                _continueIcon.gameObject.SetActive(false);
                _passwordIcon.gameObject.SetActive(true);
                break;
            default:
                break;
        }
        _selectIndex = i;
    }

    public void Click() 
    {
        switch (_selectIndex)
        {
            case 0:
                ContinueGame();
                break;
            case 1:
                PasswordInput();
                break;
        }
    }

    private void ContinueGame()
    {
        GameManager.Instance.LoadDefaultLevel();
    }
    private void PasswordInput()
    {
        
    }

    #region Input 
    private void AddInputEvent()
    {
        InputManager.Instance.OnSelectClickEvent += OnSelectClicked;
        InputManager.Instance.OnStartClickEvent += OnStartClicked;
    }
    private void RemoveInputEvent()
    {
        InputManager.Instance.OnSelectClickEvent -= OnSelectClicked;
        InputManager.Instance.OnStartClickEvent -= OnStartClicked;
    }
    private void OnSelectClicked()
    {
        if (_selectIndex == 0)
        {
            _selectIndex = 1;
        }
        else if (_selectIndex == 1)
        {
            _selectIndex = 0;
        }
        Show(_selectIndex);
    }
    private void OnStartClicked()
    {
        Click();
    }
    #endregion
}
